function afterOpen( s_obj, oc_obj){
	var open_elements = oc_obj.getOpenElements();
	var close_elements = oc_obj.getCloseElements();
	var len = Math.max( open_elements.length, close_elements.length);
	for( var i=0; i<len; i++){
		toggleHeaderElements( open_elements[i], close_elements[i], 1);
	}
}
function afterClose( s_obj, oc_obj){
	var open_elements = oc_obj.getOpenElements();
	var close_elements = oc_obj.getCloseElements();
	var len = Math.max( open_elements.length, close_elements.length);
	for( var i=0; i<len; i++){
		toggleHeaderElements( open_elements[i], close_elements[i], 0);
	}
}

function toggleHeaderElements( elm_open, elm_close, open){
	if( open ){
		if( elm_close ) elm_close.show();
		if( elm_open ) elm_open.hide();
	}else{
		if( elm_open ) elm_open.show();
		if( elm_close ) elm_close.hide();
	}
}

/*
 * this function should have all the code to process animatable elements for JRS
 */
function processAnimatableElements(elm){
	elm = $(elm) || $$('body')[0];
	PSWrap.Debug.printToConsole('processAnimatableElements: ' + elm);
	var temp, l;
	var func = Element.select;
	var anim_obj = null;
	
	temp = func( elm, '.slide_element');
	l = temp.length;
	anim_obj = PSWrap.Animation.OpenCloseIDBound;
	for( var i=0; i<temp.length; i++){
		new anim_obj(
					temp[i], 
					{duration:0.75, transition: Effect.Transitions.sinoidal, afterLoad: dynamicLoadAnimatableAfterLoad},
					{
						effect: Effect.BlindDown,
						options: {
						beforeStart: afterOpen
					}},
					{
						effect: Effect.BlindUp,
						options: {
						beforeStart: afterClose
				}});
	}
	
	temp = func( elm, '.fade_collection');
	l = temp.length;
	anim_obj = PSWrap.Animation.OpenCloseCollection;
	for( var i=0; i<l; i++){
		new anim_obj(
					temp[i],
					{},
					{duration: 2.0, afterLoad: dynamicLoadAnimatableAfterLoad},
					{effect: Effect.Appear},
					{effect: Effect.Fade, options: {transition: Effect.Transitions.full}}
			);
	}
	
	temp = func( elm, '.slide_collection');
	l = temp.length;
	anim_obj = PSWrap.Animation.OpenCloseCollection;
	for( var i=0; i<l; i++){
		new anim_obj(temp[i], {animation_type: PSWrap.Animation.SEQUENTIAL, position_items: false}, {duration: 0.5, afterLoad: dynamicLoadAnimatableAfterLoad}, {effect: Effect.BlindDown}, {effect: Effect.BlindUp});
	}
	
	temp = func( elm, '.sequential_fade_collection');
	l = temp.length;
	anim_obj = PSWrap.Animation.SequentialOpenCloseCollection;
	for( var i=0; i<l; i++){
		new anim_obj( temp[i], {}, {duration: 1, afterLoad: dynamicLoadAnimatableAfterLoad}, {effect: Effect.Appear}, {effect: Effect.Fade});
	}
	
	temp = func( elm, '.slide_show');
	l = temp.length;
	/*anim_obj = PSWrap.Animation.MovableCollection;
	for( var i=0; i<l; i++){
		new anim_obj( temp[i], {direction: PSWrap.Animation.Directions.VERTICAL});
	}*/
	anim_obj = PSWrap.Animation.SequentialOpenCloseCollection;
	for( var i=0; i<l; i++){
		new anim_obj(
					temp[i],
					{},
					{duration: 1.0},
					{effect: Effect.Appear},
					{effect: Effect.Fade}
			);
	}
}

function processAnimatableElementsEvent( e, elm){
	processAnimatableElements(elm);
	PSWrap.State.applyState();
}

function processAnimatableElementsFromDynamicLoader(dl){
	var elms = dl.getElements();
	var l = elms.length;
	for( var i=0; i<l; i++){
		processAnimatableElements(elms[i]);
	}
}

function processActionElements(dl){
	/* want to look for _action_open, _action_close, etc. and bind them correctly */
	var elms = dl.getElements();
	var l = elms.length;
	var select = Element.select;
	var re = /((?:[^\s_]+_)+)action_([^-]+)/;
	for( var i=0; i<l; i++){
		var actions = select(elms[i], '[class*=_action_]');
		actions.each( function(e){
			var res = re.exec(e.className);
			var id = res[1].substr( 0, res[1].length - 1);
			var method = res[2];
			var elm = PSWrap.Animation.Elements.getElement(id);
			if( elm ){
				try{
					elm.bindElements(method, e, 'click');
				}catch(ex){}
			}
		});
	}
}

function dynamicLoadAnimatableAfterLoad( elm ){
	if( elm.isDynamicLoad() ){
		processAnimatableElementsFromDynamicLoader(elm.getDynamicLoader());
		processActionElements(elm.getDynamicLoader(elm.getDynamicLoader()));
	}
}

Event.observe(document, 'dom:loaded', processAnimatableElementsEvent);
